﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using KL.UFrame.Api.Extensions;
using KL.UFrame.Core.Result;
using KL.UFrame.Data.Business;
using KL.UFrame.Data.Models;
using Microsoft.AspNet.Identity;

namespace KL.UFrame.Api.ApiControllers
{
    [RoutePrefix("api/TrainningCategory")]
    public class TrainningCategoryController : K9ApiBaseController
    {
        private ITrainningBusiness _business;

        public TrainningCategoryController(ITrainningBusiness business)
        {
            _business = business;
        }

        public async Task<ResultMessage<List<TrainningCategoryModel>>> Get(bool onlyEnabled)
        {
            var model =await _business.GetAllCategoriesAsync(onlyEnabled);
            return model;
        }

        [Route("GetCategoryiesWithTrainnings")]
        public async Task<ResultMessage<object>> GetCategoryiesWithTrainnings()
        {
            var model = await _business.GetCategoryiesWithTrainnings();
            return new ResultMessage<object>(model);
        }

        [HttpPost]
        [Route("Delete")]
        public async Task<ResultMessage<bool>> Delete(int id)
        {
           return  new ResultMessage<bool>(await _business.DeleteCategoryAsync(id));
        }

        [HttpPost]
        [Route("Resume")]
        public async Task<ResultMessage<bool>> Resume(int id)
        {
            return new ResultMessage<bool>(await _business.ResumeCategoryAsync(id));
        }

        [Route("Update")]
        public async Task<ResultMessage> Update(TrainningCategoryModel model)
        {
            if (!ModelState.IsValid)
            {
                return ModelState.ToResultMessage();
            }

            model.UpdateId = User.Identity.GetUserId();
            model.UpdateTime = DateTime.Now; ;
            var id = await _business.UpdateCategoryAsync(model);
            return new ResultMessage(id);
        }

        [Route("Create")]
        public async Task<ResultMessage> Create(TrainningCategoryModel model)
        {
            if (!ModelState.IsValid)
            {
                return ModelState.ToResultMessage();
            }

            model.CreateId = User.Identity.GetUserId();
            model.CreateTime = DateTime.Now; ;
            var id = await _business.CreateCategoryAsync(model);
            return new ResultMessage(id);
        }
    }
}
